smkick = {};

smkick.Dist = 70;
smkick.Strength = 50;
smkick.Delay = 0.4;
smkick.Force = 150;

function smkick:Fire(Player)

	Player.NextKick = Player.NextKick or 0;

	local Ent = Player.SmodLeg;
	
	if !ValidEntity(Ent) then
	
		Ent = ents.Create("prop_dynamic");
		Ent:SetModel("models/weapons/v_kick.mdl");
		Ent:SetPos(Player:GetShootPos());
		Ent:SetAngles(Player:GetAimVector():Angle());
		Ent:SetMoveType(MOVETYPE_NONE);
		Ent:SetSolid(SOLID_NONE);
		Ent:SetParent(Player);
		Ent:SetNoDraw(true);
		
		Player.SmodLeg = Ent;
		
	end
	
	if Player.NextKick < CurTime() then
	
		Ent:SetParent();
		Ent:SetPos(Player:GetShootPos());
		Ent:SetAngles(Player:GetAimVector():Angle());
		Ent:SetParent(Player);
		
		Ent:SetNoDraw(false);
	
		Ent:SetPlaybackRate(2 - self.Delay);
		Ent:ResetSequence(Ent:LookupSequence(ACT_VM_PRIMARYATTACK));
		Ent:SetCycle(1);
				
		local T =
		{
		
			start = Player:GetShootPos(),
			endpos = Player:GetShootPos() + Player:GetAimVector() * self.Dist,
			filter = {Player,Ent}
		
		}
		
		T = util.TraceLine(T);
		
		if T.Hit then
		
			local Sounds =
			{
			
				"physics/body/body_medium_impact_hard6.wav",
				"physics/body/body_medium_impact_hard4.wav",
				"physics/body/body_medium_impact_hard2.wav"
				
			}
			
			Player:EmitSound(Sound(Sounds[math.random(1,#Sounds)]));

			local HEnt = T.Entity;
			
			if !HEnt:IsWorld() then
			
				HEnt:TakeDamage(self.Strength,Player,Player);
			
				if HEnt:IsNPC() then
				
					HEnt:SetVelocity(HEnt:GetVelocity() + Player:GetAimVector() * self:CalcForce(nil,T.Fraction));
				
				else
				
					local P = HEnt:GetPhysicsObject();
					
					if ValidEntity(P) then
					
						P:ApplyForceCenter(Player:GetAimVector() * self:CalcForce(P,T.Fraction));
					
					end
				
				end
			
			end
		
		else
		
			Player:EmitSound(Sound("weapons/slam/throw.wav"));
		
		end
	
		Player.NextKick = CurTime() + self.Delay;
	
		timer.Simple(self.Delay,
		function()
		
			if ValidEntity(Ent) then
			
				Ent:SetNoDraw(true);
			
			end
		
		end)
	
	end
	
end
concommand.Add("smod_kick",function(P) smkick:Fire(P) end);

function smkick:CalcForce(Phys,F)

	F = 1 - F * 0.75;

	local T = 1;
	
	if ValidEntity(Phys) then
	
		T = Phys:GetMass();
	
	end

	return math.sqrt(self.Force * (self.Dist * F)^2) * T;

end